package com.golxen.framework.user.player;


import com.golxen.framework.common.server.game.cmd.GameCommandWrapper;
import com.golxen.framework.executor.GeneralTask;
import com.golxen.utils.LogUtil;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 重置在线玩家每日数据
 *
 */
public class PlayerDataDailyResetTask extends GeneralTask {

    @Override
    public void doRun() {

        AtomicInteger count = new AtomicInteger();
        //只需在线玩家，其他玩家登录重载
        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            if (player.getState() != PlayerState.ONLINE) {
                continue;
            }

            count.incrementAndGet();
            player.putMsg(new PlayerDailyResetCmd(player, () -> {
                LogUtil.COMMON.info("每日任务定时器执行:玩家" + player.getUserId());
                try {
                    player.resetDay();
                } catch (Throwable t) {
                    LogUtil.COMMON.error("", t);
                }
                count.decrementAndGet();
            }));
        }

        while (count.get() > 0) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                LogUtil.COMMON.error("", e);
            }
        }

    }

    public static class PlayerDailyResetCmd extends GameCommandWrapper {
        public PlayerDailyResetCmd(GamePlayer player, Runnable r) {
            super(player, r);
        }
    }
}
